package com.example.lib_common.base

import android.app.Application
import cn.jiguang.api.utils.JCollectionAuth
import cn.jpush.android.api.JPushInterface
import com.alibaba.android.arouter.launcher.ARouter
import com.example.lib_common.EventBusInitializer
import com.example.lib_log.utils.LoggerUtils
import com.tencent.matrix.Matrix
import com.tencent.matrix.trace.TracePlugin
import com.tencent.matrix.trace.config.TraceConfig

import com.tencent.mmkv.MMKV
open class BaseApplication:Application() {
    companion object{
        lateinit var instance:BaseApplication
    }
    override fun onCreate() {
        super.onCreate()
        instance = this
        initARouter()
        initMMKV()
        initJPush()
        initMatrix()
        EventBusInitializer.init(this)
    }

    private fun initMatrix() {
        val builder: Matrix.Builder = Matrix.Builder(this)



        // 2.2 配置内存泄漏监控插件
        val resourceConfig = TraceConfig.Builder()
            .enableDenseMsgTracer(true) // 开启泄漏检测
            .build()

        builder.plugin(TracePlugin(resourceConfig))



        // 3. 初始化 Matrix
        Matrix.init(builder.build())


        // 4. 启动所有插件
        Matrix.with().startAllPlugins()
    }

    private fun initMMKV() {
        MMKV.initialize(this)
    }

    private fun initARouter(){
        ARouter.openLog();     // 打印日志
        ARouter.openDebug();   // 开启调试模式(如果在InstantRun模式下运行，必须开启调试模式！线上版本需要关闭,否则有安全风险)
        ARouter.init(this); // 尽可能早，推荐在Application中初始化
    }
    private fun initJPush(){
        JCollectionAuth.setAuth(this,true)
        JPushInterface.setDebugMode(true)
        JPushInterface.init(this)
    }
}